﻿//using B.S.BaseData.Domain.审批表结构;
//using B.S.BaseData.Infraturvture;
//using B.S.BaseData.Read.Api.Application.CommandHander.Workflow;
//using Microsoft.AspNetCore.Http;
//using Microsoft.AspNetCore.Mvc;
//using Newtonsoft.Json;
//using WorkflowCore.Interface;
//using WorkflowCore.Models.DefinitionStorage.v1;
//using WorkflowCore.Services.DefinitionStorage;

//namespace B.S.BaseData.Read.Api.Controllers
//{
//    [Route("[controller]/[action]")]
//    [ApiController]
//    public class WorkflowController : ControllerBase
//    {
//        private readonly IWorkflowHost _workflowHost;
//        private readonly PreWorkflowDbContext _db;
//        private readonly IDefinitionLoader _definitionLoader;
//        private readonly IWorkflowRegistry _workflowRegistry;
//        public WorkflowController(IWorkflowHost workflowHost
//        , PreWorkflowDbContext db
//        //, IDefinitionLoader definitionLoader
//        , IWorkflowRegistry workflowRegistry)
//        {
//            _workflowHost = workflowHost;
//            _db = db;
//            _definitionLoader = definitionLoader;
//            _workflowRegistry = workflowRegistry;
//        }

//        /// <summary>
//        /// 定义流程
//        /// </summary>
//        /// <param name="input"></param>
//        /// <returns></returns>
//        [HttpPost]
//        public IActionResult AddWorkflowDefinition([FromBody]
//WorkflowDefinitionInput input)
//        {
//            List<WorkflowStepNode> nodes = new();
//            PersistedWorkflowDefinition workflowDefinition = new()
//            {
//                Id = Guid.NewGuid(),
//                WorkflowDefinitionId = input.WorkflowDefinitionId,
//                Version = input.Version,
//                Title = input.Title
//            };
//            if (input.Steps.Any())
//            {
//                nodes.Add(new()
//                {
//                    Id = nameof(StartStep),
//                    Name = "开始步骤",
//                    NextStepId = input.Steps.First().StepId,
//                    StepType = $"{typeof(StartStep).FullName}, {typeof(StartStep).Assembly.FullName}"
//                });
//                foreach (var step in input.Steps)
//                {
//                    nodes.Add(new()
//                    {
//                        Id = step.StepId,
//                        Name = step.StepName,
//                        NextStepId = step.NextStepId!,
//                        StepType = $"{typeof(AuditStep).FullName},{typeof(AuditStep).Assembly.FullName}"
//                    });
//                }
//                nodes.Add(new()
//                {
//                    Id = nameof(EndStep),
//                    Name = "结束步骤",
//                    StepType = $"{typeof(EndStep).FullName},{typeof(EndStep).Assembly.FullName}"
//                });
//                workflowDefinition.StepNodes = JsonConvert.SerializeObject(nodes);
//            }
//            _db.PersistedWorkflowDefinitions.Add(workflowDefinition);
//            _db.SaveChanges();
//            // 注册流程
//            LoadDefinition(workflowDefinition.WorkflowDefinitionId);
//            return Ok(workflowDefinition);
//        }


//        /// <summary>
//        /// 流程发起
//        /// </summary>
//        /// <param name="input"></param>
//        /// <returns></returns>
//        [HttpPost]
//        public IActionResult Start([FromBody] WorkflowLaunchInput input)
//        {
//            LoadDefinition(input.WorkflowDefinitionId);
//            var dict = JsonConvert.DeserializeObject<Dictionary<string, Object>>(
//            JsonConvert.SerializeObject(input));
//            var workflowId =
//            _workflowHost.StartWorkflow(input.WorkflowDefinitionId, input.Version, dict);
//            return Ok("流程发起成功");
//        }
//        private void LoadDefinition(string workflowDefinitionId)
//        {
//            var workflowDefinition = _db.PersistedWorkflowDefinitions
//            .FirstOrDefault(p => p.WorkflowDefinitionId == workflowDefinitionId);
//            if (_workflowRegistry
//            .IsRegistered(workflowDefinition.WorkflowDefinitionId, workflowDefinition.Version
//            ))
//            {
//                return;
//            }
//            // 流程定义
//            DefinitionSourceV1 source = new()
//            {
//                Id = workflowDefinition.WorkflowDefinitionId,
//                DataType = $"{typeof(Dictionary<string, object>).FullName},{typeof(Dictionary<string, object>).Assembly.FullName}",
//                Version = workflowDefinition.Version,
//                Description = workflowDefinition.Title
//            };
//            // 构造流程执行步骤
//            var stepNodes = JsonConvert
//            .DeserializeObject<List<WorkflowStepNode>>
//            (workflowDefinition.StepNodes);
//            foreach (var stepNode in stepNodes)
//            {
//                StepSourceV1 stepSource = new()
//                {
//                    StepType = stepNode.StepType,
//                    NextStepId = stepNode.NextStepId,
//                    Name = stepNode.Name,
//                    Id = stepNode.Id
//                };
//                source.Steps.Add(stepSource);
//            }
//            var json = JsonConvert.SerializeObject(source);
//            // 注册流程
//            _definitionLoader.LoadDefinition(json, Deserializers.Json);
//        }
//    }
//}
